package cn.stylefeng.guns.sys.core.listener;

import cn.stylefeng.guns.sys.core.util.UUIDUtils;
import cn.stylefeng.guns.sys.modular.system.entity.BtyDysk;
import cn.stylefeng.guns.sys.modular.system.service.BtyDyskService;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Slf4j
/*
 * 监听器批处理数据
 * */
public class DyskExcelListener extends AnalysisEventListener<BtyDysk> {

    private BtyDyskService dyskService;

    public DyskExcelListener(){

    }

    public DyskExcelListener(BtyDyskService dyskService){
        this.dyskService = dyskService;
    }


    /**
     * 批处理阈值2000
     */
    private static final int BATCH_COUNT = 100;
    List<BtyDysk> list = new ArrayList<BtyDysk>();

    /**
     *
     * @param basic
     * @param analysisContext
     */
    @Override
    public void invoke(BtyDysk basic, AnalysisContext analysisContext) {
        String mdid=basic.getMdid().replaceAll("\\s*", "");
        if(mdid.equals("6925910912494733323")){
            mdid="6839540870291720199";
        }else if(mdid.equals("6896593897577777165")){
            mdid="6699616844141168652";
        }else if(mdid.equals("7014296847938226206")){
          mdid="7014448283317700616";
        }else if(mdid.equals("7024101783576250381")){
          mdid="7024101743189313544";
        }
        basic.setMdid(mdid);
        String hxsj=basic.getHxsj().substring(0,10);
        basic.setHxsj(hxsj);
        basic.setId(UUIDUtils.getId());
        list.add(basic);
        if (list.size() >= BATCH_COUNT) {
            saveData();
            list.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        analysisContext.readWorkbookHolder().setDefaultReturnMap(false);
        saveData();
        // log.info("所有数据解析完成！");

    }
    private void saveData(){
        //调用saveData()方法
        dyskService.insertDysk(list);
    }
}